#@layer()

#define css()
<link href="#(CPATH)/static/components/jquery-cropper/cropper.css" rel="stylesheet">
<link href="#(CPATH)/static/components/jquery-file-upload/css/jquery.fileupload.css" rel="stylesheet">
<style>

    .nav-tabs .nav-link {
        border-top-left-radius: 0;
        border-top-right-radius: 0;
    }

    .attachment_item {
        height: 220px;
        width: 200px;
        padding: 0px;
        border: 1px solid #ddd;
        margin: 10px;
        float: left;
        cursor: pointer;
    }

    .attachment_item:hover {
        border: 1px solid #3c8dbc;
    }

    .attachment_item img {
        height: 90%;
        min-height: 90%;
        width: 100%;
    }

    .attachment_item_title {
        height: 10%;
        background-color: #ecf0f5;
        text-align: center;
        padding: 0 5px;
        overflow: hidden;
    }

    .attachment_item_title p {
        overflow: hidden;
    }

    .my-row {
        display: flex;
        flex-wrap: wrap;
        justify-content: center;
    }

    #uploader {
        height: 230px;
    }

    .myPanel {
        font-size: 25px;
        color: #ccc;
        text-align: center;
        padding-top: 60px;
    }

    .upload-main {
        background-color: #ddd;
        width: 100%;
        height: 370px;
    }

    .preview-content {
        display: flex;
        flex-direction: column;
        height: 100%;
        justify-content: space-between;
    }

    .upload-main img,.preview-content img {
        width: 100%;
    }

    .preview-big {
        width: 170px;
        height: 170px;
    }

    .preview-second {
        width: 100px;
        height: 100px;
    }

    .preview-small {
        width: 50px;
        height: 50px;
    }

    .img-preview {
        float: left;
        margin-bottom: .5rem;
        margin-right: .5rem;
        overflow: hidden;
    }

    .upload-main > img, .img-preview > img {
        max-width: 100%;
    }

    .cropper-container,
    .img-preview {
        background-color: #ddd;
        text-align: center;
    }

    .cropper-container {
        margin-bottom: 1rem;
        max-height: 497px;
        min-height: 200px;
        width: 100% !important;
    }

    .tab-content > .tab-pane {
        padding: 1rem;
    }

    .card.card-outline-tabs .card-header a {
        border-top-color: #dee2e6;
    }


</style>
#end

#define script()
<script src="#(CPATH)/static//components/jquery-cropper/cropper.js"></script><!-- Cropper.js is required -->
<script src="#(CPATH)/static//components/jquery-cropper/jquery-cropper.js"></script>
<script src="#(CPATH)/static/components/jquery-file-upload/js/vendor/jquery.ui.widget.js"></script>
<script src="#(CPATH)/static/components/jquery-file-upload/js/jquery.iframe-transport.js"></script>
<script src="#(CPATH)/static/components/jquery-file-upload/js/jquery.fileupload.js"></script>
<script>

    //clear layer.data
    parent.layer.data = {};

    $('#cfile').fileupload({
        dropZone: $('#uploader'),
        url: '#(CPATH)/commons/attachment/upload',
        sequentialUploads: true,
        progressall: function (e, data) {
            var progress = parseInt(data.loaded / data.total * 100, 10);
            $(".myPanel").text("上传进度：" + progress + "%")
        },
        done: function (e, data) {
            $(".myPanel").text("或者拖动文件到这里进行上传...");
            if (data.result.state == "ok") {
                if (data.result.cropEnable) {
                    initCropper(data)
                }else {
                    parent.layer.data.src = data.result.src;
                    parent.layer.data.title = data.result.title;
                    parent.layer.closeAll();
                }
            } else {
                showErrorMessage(data.result.message);
            }
        }
    });


    $(".attachment_item").click(function () {
        var src = $(this).children().attr("data-path");
        parent.layer.data.src = src;
        parent.layer.data.title = $(this).children().attr("title");
        parent.layer.closeAll();
    })


    var cropperImage;

    function initCropper(data) {
        $('#path').val(data.result.src);
        $('#title').val(data.result.title);

        $('#uploadedImg').attr('src', data.result.src);
        $('#path').val(data.result.src);
        $('#attachmentId').val(data.result.attachmentId);

        $(".cropper").show();
        $("#uploader").height("530px")
        $(".myPanel").hide();


        if (cropperImage) {
            cropperImage.cropper('destroy');
        }

        cropperImage = $('#uploadedImg');

        cropperImage.cropper({
            // aspectRatio: 16 / 9,
            // viewMode:3,
            preview: '.img-preview',
            crop: function (event) {
                updateSelect(event.detail);
            }
        });
    }

    function updateSelect(data) {
        $('#x').val(data.x);
        $('#y').val(data.y);
        $('#w').val(data.width);
        $('#h').val(data.height);
        // $('#zoom').val(data.h);
    }

    $('.aspect-ratio').on('click', function () {
        cropperImage.cropper('setAspectRatio', $(this).attr('data-aspect-ratio'));
    })

    function saveSuccess(data) {
        if (parent && parent.layer) {
            parent.layer.data.src = data.src;
            parent.layer.data.title = $('#title').val();
            parent.layer.closeAll();
        }
    }

    $('.cancel').on('click', function () {
        if (parent && parent.layer) {
            parent.layer.data.src = $('#path').val();
            parent.layer.data.title = $('#title').val();
            parent.layer.closeAll();
        }
    })


</script>
#end

#define content()

<div class="card card-outline card-tabs" style="box-shadow: none;">
    <input type="hidden" id="title">
    <div class="card-header p-0 border-bottom-0">
        <ul class="nav nav-tabs">
            <li class="nav-item"><a class="nav-link" href="#timeline" data-toggle="tab">上传#para("uititle","图片")</a></li>
            <li class="nav-item"><a class="nav-link #activeIf(para('categoryId') == null)" href="#activity"
                                    data-toggle="tab">#para("uititle","图片")库</a></li>

            #for(item : categories ??)
            <li class="nav-item">
                <a class="nav-link #activeIf(para('categoryId') == item.id)"
                   href="#(CPATH)/admin/attachment/browse?categoryId=#(item.id ??)&uititle=附件"> #(item.title ??)
                </a>
            </li>
            #end
        </ul>

    </div>

    <div class="card-body">
        <form action="#(CPATH)/commons/attachment/doSaveImage" class="autoAjaxSubmit" method="post"
              data-ok-function="saveSuccess">

            <input type="hidden" name="attachmentId" id="attachmentId">
            <input type="hidden" name="path" id="path">
            <input type="hidden" name="x" id="x">
            <input type="hidden" name="y" id="y">
            <input type="hidden" name="w" id="w">
            <input type="hidden" name="h" id="h">
            <input type="hidden" name="zoom" id="zoom">

            <div class="tab-content" style="padding-top:20px;">
            <div class="tab-pane" id="timeline">
                <div id="uploader">
                    <span class="btn btn-block btn-primary fileinput-button" style="width: 160px;">
                        <i class="fas fa-plus"></i>
                        <span>选择文件...</span>
                        <input id="cfile" type="file" name="files[]" multiple>
                    </span>

                    <div class="myPanel">
                        或者拖动文件到这里进行上传...
                    </div>

                    <div class="row cropper" style="display: none;padding-top: 20px">
                        <div class="col-sm-9">
                            <div class="upload-main">
                                <img src="" id="uploadedImg"/>
                            </div>
                        </div>
                        <div class="col-sm-3">
                            <div class="preview-content">
                                <div class="img-preview preview-big"></div>
                                <div class="img-preview preview-second"></div>
                                <div class="img-preview preview-small"></div>
                            </div>
                        </div>

                        <div class="col-12 row">
                            <div class="col-sm-6">
                                剪切比例：
                                <a class="mr-3 aspect-ratio" href="javascript:;" data-aspect-ratio="1">1:1</a>
                                <a class="mr-3 aspect-ratio" href="javascript:;"
                                   data-aspect-ratio="1.7777777777777777">16:9</a>
                                <a class="mr-3 aspect-ratio" href="javascript:;"
                                   data-aspect-ratio="1.3333333333333333">4:3</a>
                                <a class="mr-3 aspect-ratio" href="javascript:;"
                                   data-aspect-ratio="0.6666666666666666">2:3</a>
                                <a class="mr-3 aspect-ratio" href="javascript:;" data-aspect-ratio="NaN">自定义</a>
                            </div>
                            <div class="col-sm-6">
                                <div class="row">
                                    <div class="col-sm-12">
                                        <button type="submit" class="btn btn-primary float-right mr-2">
                                            <i class="fa fa-fw fa-check"></i> 确定
                                        </button>
                                        <button type="button"
                                                class="btn btn-outline-secondary float-right mr-2 cancel">
                                            <i class="fa fa-fw fa-times"></i> 不剪切
                                        </button>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>


                </div>
            </div>

            <div class="active tab-pane" id="activity">
                <div class="my-row">
                    #for(attachment : page.list)
                    <div class="attachment_item" data-aid="#(attachment.id ??)">
                        #if(attachment.isImage())
                        <img src="#(attachment.domainPath ??)"
                             data-path="#(attachment.domainPath ??)"
                             title="#(attachment.title ??)"
                             alt="#(attachment.title ??)">
                        #elseif(attachment.isDoc())
                        <img src="#(CPATH)/static/commons/img/suffix/doc.png"
                             data-path="#(attachment.domainPath ??)"
                             title="#(attachment.title ??)"
                             alt="#(attachment.title ??)">
                        #elseif(attachment.isXls())
                        <img src="#(CPATH)/static/commons/img/suffix/xls.png"
                             data-path="#(attachment.domainPath ??)"
                             title="#(attachment.title ??)"
                             alt="#(attachment.title ??)">
                        #elseif(attachment.isPpt())
                        <img src="#(CPATH)/static/commons/img/suffix/ppt.png"
                             data-path="#(attachment.domainPath ??)"
                             title="#(attachment.title ??)"
                             alt="#(attachment.title ??)">
                        #elseif(attachment.isTxt())
                        <img src="#(CPATH)/static/commons/img/suffix/txt.png"
                             data-path="#(attachment.domainPath ??)"
                             title="#(attachment.title ??)"
                             alt="#(attachment.title ??)">
                        #elseif(attachment.isZip())
                        <img src="#(CPATH)/static/commons/img/suffix/zip.png"
                             data-path="#(attachment.domainPath ??)"
                             title="#(attachment.title ??)"
                             alt="#(attachment.title ??)">
                        #elseif(attachment.isRar())
                        <img src="#(CPATH)/static/commons/img/suffix/rar.png"
                             data-path="#(attachment.domainPath ??)"
                             title="#(attachment.title ??)"
                             alt="#(attachment.title ??)">
                        #elseif(attachment.isExe())
                        <img src="#(CPATH)/static/commons/img/suffix/exe.png"
                             data-path="#(attachment.domainPath ??)"
                             title="#(attachment.title ??)"
                             alt="#(attachment.title ??)">
                        #elseif(attachment.isPdf())
                        <img src="#(CPATH)/static/commons/img/suffix/pdf.png"
                             data-path="#(attachment.domainPath ??)"
                             title="#(attachment.title ??)"
                             alt="#(attachment.title ??)">
                        #elseif(attachment.isAudio())
                        <img src="#(CPATH)/static/commons/img/suffix/audio.png"
                             data-path="#(attachment.domainPath ??)"
                             title="#(attachment.title ??)"
                             alt="#(attachment.title ??)">
                        #elseif(attachment.isVideo())
                        <img src="#(CPATH)/static/commons/img/suffix/video.png"
                             data-path="#(attachment.domainPath ??)"
                             title="#(attachment.title ??)"
                             alt="#(attachment.title ??)">
                        #else
                        <img src="#(CPATH)/static/commons/img/suffix/unknow.png"
                             data-path="#(attachment.domainPath ??)"
                             title="#(attachment.title ??)"
                             alt="#(attachment.title ??)">
                        #end
                        <div class="attachment_item_title">
                            <p>#(attachment.title ??)</p>
                        </div>
                    </div>

                    #end
                </div>

                <div class="row" style="padding-right: 20px;padding-top: 30px">
                    <div class="col-12">
                        #@_paginate()
                    </div>
                </div>
            </div>
        </div>
        </form>
    </div>
</div>

#end
